package com.zyh.generic;

import com.zyh.generic.test.domain.Student;
import com.zyh.generic.test.domain.Teacher;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @Author zyh
 * @Date 2024/7/31 11:00
 * @Description
 */

public class GenericDemo3 {
    public static void main(String[] args) {
        //年龄从小到大排序
        ArrayList<Student> list = new ArrayList<>();
        list.add(new Student("liusan",29,90.0));
        list.add(new Student("lisi",45,90.0));
        list.add(new Student("wangwu",50,99.0));
        list.add(new Student("sunliu",15,100.0));
        ComparableImpl1 comparableImpl1 = new ComparableImpl1();
        List<Student> newList = comparableImpl1.sortAge(list);
        System.out.println("newList = " + newList);

        //分数从大到小排序
        LinkedList<Teacher> list1=new LinkedList<>();
        list1.add(new Teacher("liusan",29,95.4));
        list1.add(new Teacher("lisi",45,90.0));
        list1.add(new Teacher("wangwu",50,56.15));
        list1.add(new Teacher("sunliu",15,79.36));
        ComparableImpl2 comparableImpl2 = new ComparableImpl2();
        List<Teacher> newList2 = comparableImpl2.sortScore(list1);
        System.out.println("newList2 = " + newList2);
    }
}

class ComparableImpl2 implements Comparable<Teacher>{

    @Override
    public List<Teacher> sortAge(List<Teacher> list) {
        return null;
    }

    //根据分数从大到小排序
    @Override
    public List<Teacher> sortScore(List<Teacher> list) {
        for (int i = 0; i < list.size() - 1; i++) {
            for (int j = 0; j < list.size() - 1 - i; j++) {
                if (list.get(j).getScore()<list.get(j+1).getScore()){
                    Teacher temp=list.get(j);
                    list.set(j,list.get(j+1));
                    list.set(j+1,temp);
                }
            }
        }
        return list;
    }
}


class ComparableImpl1 implements Comparable<Student>{

    //根据年龄从小到大排序
    @Override
    public List<Student> sortAge(List<Student> list) {
        for (int i = 0; i < list.size() - 1; i++) {
            for (int j = 0; j < list.size() - 1 - i; j++) {
                if (list.get(j).getAge()>list.get(j+1).getAge()){
                    Student temp=list.get(j);
                    list.set(j,list.get(j+1));
                    list.set(j+1,temp);
                }
            }
        }
        return list;
    }

    @Override
    public List<Student> sortScore(List<Student> list) {
        return null;
    }
}

interface Comparable<T>{
    List<T> sortAge(List<T> list);
    List<T> sortScore(List<T> list);
}
